//
//  ViewController.m
//  ZHReactiveObjcDemo
//
//  Created by 赵宏亚 on 2019/8/15.
//  Copyright © 2019 ArthurRoom. All rights reserved.
//

#import "ViewController.h"
#import <NSObject+RACKVOWrapper.h>
#import "Person.h"
#import <ReactiveObjC.h>
#import "Model.h"
#import "BlueView.h"


@interface ViewController ()

@property (nonatomic,strong) Person *p;

@property (weak, nonatomic) IBOutlet UIButton *button;
@property (weak, nonatomic) IBOutlet UITextField *textField;

@property (weak, nonatomic) IBOutlet BlueView *blueView;



@end

@implementation ViewController

- (void)rac_changyongfangfa {
    _p = [[Person alloc] init];
    
    // RAC的的KVO
    [_p rac_observeKeyPath:NSStringFromSelector(@selector(name)) options:NSKeyValueObservingOptionNew observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
        
        NSLog(@"value:%@",value);
    }];
    
    
    // KVO
    [[_p rac_valuesForKeyPath:NSStringFromSelector(@selector(name)) observer:nil] subscribeNext:^(id  _Nullable x) {
        
        NSLog(@"rac **** %@",x);
    }];
    
    // 按钮点击事件
    [[_button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        NSLog(@"点击了这里");
    }];
    
    // 代替通知
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:UIKeyboardDidShowNotification object:nil] subscribeNext:^(NSNotification * _Nullable x) {
        NSLog(@"监控键盘 *** %@",x);
    }];
    
    // 文本框输入监控
    [_textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"文本框监听 *** %@",x);
    }];
}

// 用于Model
- (void)rac_model {
    NSArray *dicArr = @[@{@"name":@"张三",@"age":@"13"},@{@"name":@"李四",@"age":@"14"}];
    
    
    NSMutableArray *arr = [NSMutableArray array];
    // 方法1：常规遍历数组
    [dicArr.rac_sequence.signal subscribeNext:^(NSDictionary *x) {
        
        [arr addObject:[Model modelWithDic:x]];
    }];
    
    // 方法2：使用map进行映射
    // 数组转sequence -> map映射元素dic -> 将dic转为模型返回为类型RACSequence -> 将 RACSequence 转为数组。
    NSArray *arrMap = [[dicArr.rac_sequence map:^id _Nullable(NSDictionary *value) {
        // 返回模型
        return  [Model modelWithDic:value];
        
    }] array];
    
    NSLog(@"arrMap ***** %@",arrMap);
}

- (void)rac_delegate {
    [[_blueView rac_signalForSelector:@selector(btnClick:)] subscribeNext:^(RACTuple * _Nullable x) {
        
        // RACTuple 里存放的为方法的参数
        NSLog(@"blueView ***** %@",x[0]);
    }];
}

- (void)rac_kvo {
    // 代替KVO，可以设置新/旧值监控
    [_blueView rac_observeKeyPath:@"frame" options:NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew observer:nil block:^(id value, NSDictionary *change, BOOL causedByDealloc, BOOL affectedOnlyLastComponent) {
        // 回调
        
        NSLog(@"value:%@\nchange:%@",value,change);
    }];
    
    // 监听blueView下的属性
    [[_blueView rac_valuesForKeyPath:@"frame" observer:self] subscribeNext:^(id  _Nullable x) {
        // 返回的是最新值
        NSLog(@"rac_valuesForKeyPath **** %@",x);
    }];
}

- (void)rac_event {
    [[_button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        
        // 直接监听按钮的点击事件
        NSLog(@"点击了按钮  *** %@",x);
    }];
}

- (void)rac_notification {
    [[[NSNotificationCenter defaultCenter] rac_addObserverForName:@"nihaoya" object:nil] subscribeNext:^(NSNotification * notification) {
        
        NSLog(@"通知的名字 **** %@",notification.name);
        NSLog(@"通知的传值 **** %@",notification.object);
    }];
}

- (RACDisposable *)rac_textField {
    return [_textField.rac_textSignal subscribeNext:^(NSString * _Nullable x) {
        NSLog(@"文本框内容 *** %@",x);
    }];
}

- (void)rac_subject {
    // 1.创建信号
    RACSubject * subject = [RACSubject subject];
    
    // 2.订阅信号
    [subject subscribeNext:^(id  _Nullable x) {
        NSLog(@"subject *** %@",x);
    }];
    
    // 3.发送信号
    [subject sendNext:@"nihaoya"];
}

- (RACDisposable * _Nonnull)rac_timer {
    // 定时器
    return [[RACSignal interval:1.0 onScheduler:[RACScheduler scheduler]] subscribeNext:^(NSDate * _Nullable x) {
        NSLog(@"%@ ---- %@",x,[NSThread currentThread]);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self rac_changyongfangfa]; // 常用方法
    [self tupleUse]; // 元祖使用
    [self rac_shuzu]; // 数组使用
    [self rac_dic]; // 字典使用
    [self rac_model]; // 转Model
    [self rac_delegate]; // 代替代理
    [self rac_kvo]; // 代替kvo
    [self rac_event]; // 监听事件
    [self rac_notification]; // 代替通知接收
    [self rac_textField]; // 监听文本框
    [self rac_subject]; // 使用subject
//    [self rac_timer]; // timer创建
    
    
    
    
}

// 元祖使用
- (void)tupleUse {
    
    // RACTuple元祖
    RACTuple *tuple = [RACTuple tupleWithObjectsFromArray:@[@"aaaaa",@"bbbb",@123]];
    NSString *str = tuple[0];
    NSLog(@"%@",str);
    
}

//RACSequence:用于代替NSArray，NSDictionary，可以使用快速的遍历
// 常见的应用场景：字典转模型

// 数组
- (void)rac_shuzu {
    // 数组
    NSArray *arr = @[@"abc",@"bbb",@123];
    //    // RAC集合
    //    RACSequence *sequence = arr.rac_sequence;
    //    // 遍历
    //    RACSignal *signal = sequence.signal;
    //
    //    // 订阅信号
    //    [signal subscribeNext:^(id  _Nullable x) {
    //        NSLog(@"遍历所有元素 %@",x);
    //    }];
    [arr.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
        NSLog(@"遍历所有元素 %@",x);
    }];
}

- (void)rac_dic {
    
    NSDictionary *dic = @{@"name":@"zhangsuan",@"age":@"18"};
    
    // 字典转集合
    [dic.rac_sequence.signal subscribeNext:^(id  _Nullable x) {
//        NSString *key = x[0];
//        NSString *value = x[1];
//        NSLog(@"key === %@ value ==== %@",key,value);
        
        // RACTupleUnpack:用来解析元祖
        // 宏里面的参数，传需要解析出来的变量名称
        RACTupleUnpack(NSString *key,NSString *value) = x;
        NSLog(@"key === %@ value ==== %@",key,value);
        
        
        
    }];
    
    
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    
    static int a = 50;
//    self.p.name = [NSString stringWithFormat:@"%d",a++];
    a ++;
    self.blueView.frame = CGRectMake(a, 100, 200, 200);
    
    // 发起通知
    [[NSNotificationCenter defaultCenter] postNotificationName:@"nihaoya" object:@[@1,@2,@3]];
}




@end
